package com.agriculture.competition.service.impl;

import com.agriculture.common.core.utils.DateUtils;
import com.agriculture.common.core.utils.StringUtils;
import com.agriculture.competition.domain.JaClassify;
import com.agriculture.competition.domain.JaFile;
import com.agriculture.competition.domain.vo.ExcelData;
import com.agriculture.competition.mapper.JaClassifyMapper;
import com.agriculture.competition.mapper.JaDateTableMapper;
import com.agriculture.competition.service.IJaClassifyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * 分类管理Service业务层处理
 *
 * @author agriculture
 * @date 2024-11-13
 */
@Service
@Slf4j
public class JaClassifyServiceImpl implements IJaClassifyService {

    @Autowired
    private JaClassifyMapper jaClassifyMapper;

    @Autowired
    private JaDateTableMapper jaDateTableMapper;
    @Autowired
    private ExcelReadUtils excelReadUtils;
    /**
     * 查询分类管理
     *
     * @param id 分类管理主键
     * @return 分类管理
     */
    @Override
    public JaClassify selectJaClassifyById(Long id) {
        return jaClassifyMapper.selectJaClassifyById(id);
    }

    /**
     * 查询分类管理列表
     *
     * @param jaClassify 分类管理
     * @return 分类管理
     */
    @Override
    public List<JaClassify> selectJaClassifyList(JaClassify jaClassify) {
        List<JaClassify> jaClassifies = jaClassifyMapper.selectJaClassifyList(jaClassify);
        if (!CollectionUtils.isEmpty(jaClassifies)) {
            jaClassifies.sort(Comparator.comparing(JaClassify::getOrderNum, Comparator.nullsLast(Comparator.naturalOrder())));
        }
        return jaClassifies;
    }

    /**
     * 新增分类管理
     *
     * @param jaClassify 分类管理
     * @return 结果
     */
    @Override
    public int insertJaClassify(JaClassify jaClassify) {
        jaClassify.setCreateTime(DateUtils.getNowDate());
        return jaClassifyMapper.insertJaClassify(jaClassify);
    }

    /**
     * 修改分类管理
     *
     * @param jaClassify 分类管理
     * @return 结果
     */
    @Override
    public int updateJaClassify(JaClassify jaClassify) {
        jaClassify.setUpdateTime(DateUtils.getNowDate());
        return jaClassifyMapper.updateJaClassify(jaClassify);
    }

    /**
     * 批量删除分类管理
     *
     * @param ids 需要删除的分类管理主键
     * @return 结果
     */
    @Override
    public int deleteJaClassifyByIds(Long[] ids) {
        return jaClassifyMapper.deleteJaClassifyByIds(ids);
    }

    /**
     * 删除分类管理信息
     *
     * @param id 分类管理主键
     * @return 结果
     */
    @Override
    public int deleteJaClassifyById(Long id) {
        return jaClassifyMapper.deleteJaClassifyById(id);
    }

    @Override
    public List<JaClassify> queryTree(JaClassify jaClassify) {
        if ((StringUtils.isEmpty(jaClassify.getName()) && StringUtils.isEmpty(jaClassify.getDescribeInfo()))) {
            jaClassify.setLeval(0L);
        }
        List<JaClassify> jaClassifies = jaClassifyMapper.selectJaClassifyList(jaClassify);
        if (!CollectionUtils.isEmpty(jaClassifies)) {
            jaClassifies.sort(Comparator.comparing(JaClassify::getOrderNum, Comparator.nullsLast(Comparator.naturalOrder())));
        }
        JaClassify jaClassify1 = new JaClassify();
        jaClassify1.setDataType(jaClassify.getDataType());
        List<JaClassify> all = jaClassifyMapper.selectJaClassifyList(jaClassify1);
        List<JaClassify> treeResult = new ArrayList<>();
        for (JaClassify jaClassify2 : jaClassifies) {
            List<JaClassify> tree = getTrees(all, jaClassify2);
            jaClassify2.setChilds(tree);
            treeResult.add(jaClassify2);
        }
        return treeResult;
    }

    @Override
    public List<JaClassify> queryModelTree(JaClassify jaClassify) {
        jaClassify.setLeval(0L);
        jaClassify.setDataType(jaClassify.getDataType());
        List<JaClassify> jaClassifies = jaClassifyMapper.selectJaClassifyList(jaClassify);
        if (!CollectionUtils.isEmpty(jaClassifies)) {
            jaClassifies.sort(Comparator.comparing(JaClassify::getOrderNum, Comparator.nullsLast(Comparator.naturalOrder())));
        }
        JaClassify jaClassify1 = new JaClassify();
        jaClassify1.setDataType(jaClassify.getDataType());
        List<JaClassify> all = jaClassifyMapper.selectJaClassifyList(jaClassify1);
        List<JaClassify> treeResult = new ArrayList<>();
        for (JaClassify jaClassify2 : jaClassifies) {
            List<JaClassify> tree = getTrees(all, jaClassify2);
            jaClassify2.setChilds(tree);
            treeResult.add(jaClassify2);
        }
        return treeResult;
    }

    @Override
    public List<JaClassify> queryDataTree(JaClassify jaClassify) {
        JaClassify jaClassifyquery = new JaClassify();
        jaClassifyquery.setDataType(jaClassify.getDataType());
        List<JaClassify> jaClassifies1 = jaClassifyMapper.selectJaClassifyList(jaClassifyquery);
        List<JaClassify> all = new ArrayList<>();
        if (!CollectionUtils.isEmpty(jaClassifies1)) {
            for (JaClassify classify : jaClassifies1) {
                List<JaFile> strings = jaDateTableMapper.queryJaDateTableByclassifId(classify.getId());
                if (!CollectionUtils.isEmpty(strings)) {
                    for (JaFile file : strings) {
                        if (file != null) {
                            JaClassify jaClassify2 = new JaClassify();
                            jaClassify2.setParentId(classify.getId());
                            jaClassify2.setId(file.getExcelId());
                            jaClassify2.setName(file.getFileName());
                            jaClassify2.setFileUrl(file.getFileUrl());
                            jaClassify2.setTableId(file.getTableId());
                            jaClassify2.setDisplayName(classify.getDisplayName());
                            all.add(jaClassify2);
                        }
                    }

                }
            }
        }
        jaClassify.setLeval(0L);
        jaClassify.setDataType(jaClassify.getDataType());
        List<JaClassify> jaClassifies = jaClassifyMapper.selectJaClassifyList(jaClassify);
        JaClassify jaClassify1 = new JaClassify();
        jaClassify1.setDataType(jaClassify.getDataType());
        List<JaClassify> all1 = jaClassifyMapper.selectJaClassifyList(jaClassify1);
        all.addAll(all1);
        List<JaClassify> treeResult = new ArrayList<>();
        for (JaClassify jaClassify2 : jaClassifies) {
            List<JaClassify> tree = getTrees(all, jaClassify2);
            jaClassify2.setChilds(tree);
            treeResult.add(jaClassify2);
        }
        return treeResult;
    }

    public Long getId() {
        Random random = new Random();
        int randomNumber = random.nextInt(900000000) + 100000000;
        return Long.valueOf(randomNumber);
    }

    @Override
    public List<JaClassify> queryDataExcelTree(JaClassify jaClassify) {
        JaClassify jaClassifyquery = new JaClassify();
        jaClassifyquery.setDataType("1");
        List<JaClassify> jaClassifies1 = jaClassifyMapper.selectJaClassifyList(jaClassifyquery);
        List<JaClassify> all = new ArrayList<>();
        if (!CollectionUtils.isEmpty(jaClassifies1)) {
            for (JaClassify classify : jaClassifies1) {
                List<JaFile> strings = jaDateTableMapper.queryJaDateTableByclassifId(classify.getId());
                if (!CollectionUtils.isEmpty(strings)) {
                    for (JaFile file : strings) {
                        if (file != null) {
                           // long l = getId();
                            JaClassify jaClassify2 = new JaClassify();
                            jaClassify2.setParentId(classify.getId());
                            jaClassify2.setId(file.getExcelId());
                            jaClassify2.setFileUrl(file.getFileUrl());
                            jaClassify2.setName(file.getFileName());
                            log.info("jaClassify2.setDisplayName" + classify.getDisplayName());
                            jaClassify2.setDisplayName(classify.getDisplayName());
                            all.add(jaClassify2);
                            ExcelData excelData = excelReadUtils.getExcelData(file.getFileUrl(),jaClassify.getTempDir());
                            if (excelData != null) {
                                Map<Integer, String> headMap = excelData.getHeadMap();
                                if (headMap != null && headMap.size() > 0) {
                                    for (Integer key : headMap.keySet()) {
                                        JaClassify jaClassify3 = new JaClassify();
                                        jaClassify3.setParentId(file.getExcelId());
                                        jaClassify3.setId(getId());
                                        jaClassify3.setFileUrl(file.getFileUrl());
                                        jaClassify3.setName(headMap.get(key));
                                        jaClassify3.setDisplayName1(classify.getDisplayName());
                                        jaClassify3.setDisplayName(file.getFileName() + "-" + headMap.get(key));
                                        all.add(jaClassify3);
                                    }
                                }
                            }
                        }

                    }
                }
            }
        }
        jaClassify.setLeval(0L);
        jaClassify.setDataType(jaClassify.getDataType());
        List<JaClassify> jaClassifies = jaClassifyMapper.selectJaClassifyList(jaClassify);
        JaClassify jaClassify1 = new JaClassify();
        jaClassify1.setDataType(jaClassify.getDataType());
        List<JaClassify> all1 = jaClassifyMapper.selectJaClassifyList(jaClassify1);
        all.addAll(all1);
        List<JaClassify> treeResult = new ArrayList<>();
        for (JaClassify jaClassify2 : jaClassifies) {
            List<JaClassify> tree = getTrees(all, jaClassify2);
            jaClassify2.setChilds(tree);
            treeResult.add(jaClassify2);
        }
        return treeResult;
    }

    @Override
    public List<JaClassify> selectJaClassifyNameByDateType(String dataType) {
        return jaClassifyMapper.selectJaClassifyNameByDateType(dataType);
    }

    public List<JaClassify> getTrees(List<JaClassify> all, JaClassify jaClassify) {
        List<JaClassify> child = new ArrayList<>();
        if (!CollectionUtils.isEmpty(all)) {
            for (JaClassify jaClassify1 : all) {
                if (jaClassify1.getParentId() != null) {
                    if (jaClassify1.getParentId().equals(jaClassify.getId())) {
                        jaClassify1.setLeval(jaClassify.getLeval() + 1L);
                        child.add(jaClassify1);
                    }
                }
            }
        }
        for (JaClassify child1 : child) {
            List<JaClassify> trees = getTrees(all, child1);
            if (!CollectionUtils.isEmpty(trees)) {
                trees.sort(Comparator.comparing(JaClassify::getOrderNum, Comparator.nullsLast(Comparator.naturalOrder())));
            }
            child1.setChilds(trees);
        }

        if (child.size() == 0) {
            return null;
        }
        return child;
    }
}

